package com.sky.controller.user;

import com.sky.entity.Address;
import com.sky.result.Result;
import com.sky.service.User.Address.AddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 褚金龙
 * @Date 2024/9/27 17:44
 */
@Api(tags = "收货地址相关接口")
@RestController
@RequestMapping("/user")
@CrossOrigin("*")
@Slf4j
public class AddressController {
    @Autowired
    private AddressService addressService;
    @PostMapping("/addressBook/add")
    @ApiModelProperty("添加收货地址")
   public Result addressSave(@RequestBody Address address){
       //log.info("{}",address);
        addressService.saveAddress(address);
        return Result.success("ok");
   }

    @GetMapping("/addressBook/list")
    @ApiModelProperty("查询所有收货地址")
    public Result addressList(){
        //log.info("{}",address);
     List<Address>  list=  addressService.addressList();
        return Result.success(list);
    }


    @GetMapping("/addressBook/{id}")
    @ApiModelProperty("根据id查询地址")
    public Result queryAddress(@PathVariable Long id){

       Address address=  addressService.getById(id);
        return Result.success(address);
    }


    @PutMapping("/addressBook/update")
    @ApiModelProperty("修改收货地址")
    public Result updateAddress(@RequestBody Address address){
        log.info("{}",address);
        addressService.updateAddress(address);
        return Result.success();
    }


    @DeleteMapping("/addressBook/delete")
    @ApiModelProperty("修改收货地址")
    public Result deleteAddress(@RequestBody Long id){

        addressService.deleteAddress(id);
        return Result.success();
    }

    @PutMapping("/addressBook/default")
    @ApiModelProperty("设置默认收货地址")
    public Result defaultAddress(@RequestBody Long id){

        addressService.defaultAddress(id);
        return Result.success();
    }

    @GetMapping("/addressBook/query")
    @ApiModelProperty("查询默认收货地址")
    public Result getDefaultAddress(){

      List<Address> list=  addressService.getDefaultAddress();
      if(list!=null&&list.size()>=1) {
          return Result.success(list.get(0));
      }

      return Result.error("没有查询到默认收货地址");
    }
}
